<template>
  <!-- 新增或修改对话框 -->
  <ElDialog
      :title="edit.title" :close-on-click-modal="false" v-model="edit.open"
             width="800px" append-to-body center>
    <el-form ref="formRef" :model="edit.data" :rules="edit.rules" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="24" v-if="!edit.data.hasChildren">
          <el-form-item label="所属参数" prop="parentId">
            <el-input v-model="edit.data.parentId"
                      :value="edit.data.parentEntityDesc+'('+edit.data.parentEntityName+')'" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-if="edit.isCopy">
        <el-col :span="24">
          <el-form-item label="所属分组" prop="cateNamespace">
            <el-select v-model="edit.data.cateNamespace" :disabled="!edit.data.hasChildren"
                       placeholder="请选择所属分组" size="small"
                       style="width: 100%;">
              <el-option v-for="dict in cateGroupCodeOptions" :key="dict.dictValue" :label="dict.dictLabel"
                         :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="参数名称" prop="entityName">
            <el-input v-model="edit.data.entityName" placeholder="请输入参数名称" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="参数类型" prop="entityClass">
            <el-select v-model="edit.data.entityClass" @change="selectEntityClass"
                       style="width: 100%;">
              <el-option v-for="item in entityClassOptions" :key="item.dictValue" :label="item.dictLabel"
                         :value="item.dictValue" placeholder="请选择参数类型" />
            </el-select>
            <!--            <el-input v-model="edit.data.entityClass" placeholder="请输入属性类名" clearable />-->
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="参数描述" prop="entityDesc">
            <el-input v-model="edit.data.entityDesc" placeholder="请输入参数描述" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <!--      <el-row :gutter="20" v-if="edit.data.isObject=='N'">-->
      <!--        <el-col :span="24">-->
      <!--          <el-form-item label="是否对象类型" prop="isObject" >-->
      <!--            <el-radio-group v-model="edit.data.isObject">-->
      <!--              <el-radio v-for="dict in isObjectOptions" :key="dict.dictValue" :label="dict.dictValue">{{dict.dictLabel}}</el-radio>-->
      <!--            </el-radio-group>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
      <el-row :gutter="20" v-if="!edit.data.hasChildren">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="edit.data.remark" placeholder="请输入备注" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}" clearable />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <dialog-footer @ok="submitForm" @cancel="cancel" />
  </ElDialog>
</template>

<script>
import {debounceWrapper} from "@/utils/zmids.ts";
import {insert, copyModel, updateById} from "@/platform/uni/api/uni/domain/index.ts";

export default {
  name: "handle",
  props: ['editForm', 'cateGroupCodeOptions', 'isLeafOptions', 'isObjectOptions', 'entityClassOptions'],
  data() {
    return {
      edit: {}
    }
  },
  mounted() {
    console.log(this.editForm)
    this.edit = this.editForm
  },
  // watch: {
  //   'edit.open': {
  //     handler:(newVal, oldVal) => {
  //       this.edit.open = newVal
  //     },
  //     immediate: true
  //   }
  // },
  // computed: {
  //   formData: {
  //     get() {
  //       const form = this.editForm?.data || {}
  //       return form || {}
  //     },
  //     set(v) {
  //       this.editForm.data = v
  //     }
  //   }
  // },
  methods: {
    selectEntityClass(v) {
      this.edit.data.isObject = v === 'object' ? 'Y' : 'N';
    },
    // 提交按钮
    submitForm: debounceWrapper(function () {
      this.$refs["formRef"].validate(valid => {
        if (valid) {
          const ifUpdate = this.edit.isModify;
          const isCopy = this.edit.isCopy;
          const promise = isCopy ? copyModel(this.edit.data) : (ifUpdate ? updateById(this.edit.data) : insert(this.edit.data));
          promise.then(response => {
            if (response.data === 1) {
              this.msgSuccess(isCopy ? "复制成功" : (ifUpdate ? '修改成功' : '新增成功'));
              this.edit.open = false;
              this.$emit('getList');
            }
          });
        }
      });
    }),
    // 取消按钮
    cancel() {
      this.edit.open = false;
      this.$emit('reset');
    },
    resetCurrentForm() {
      // 重置字段校验
      this.$refs.formRef && this.$refs.formRef.clearValidate();
      this.resetForm("formRef");
    }
  }
}
</script>

<style scoped>

</style>
